/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 */
package com.lehe.controller;

import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 公共页面错误提示
 *
 * @author 王丹
 * @since 2017-02-26
 */
@Controller
public class WebErrorController implements ErrorController {
    private static final String ERROR = "/error";

    @RequestMapping(value = ERROR)
    public String handError(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode != null && HttpStatus.NOT_FOUND.value() == statusCode) {
            if(request.getRequestURI().indexOf("admin")>=0){
                return "/exception/admin/404";
            }else {
                return "/exception/404";
            }
        }
        request.setAttribute("error", "系统繁忙，请关闭页面重试！");
        if(request.getRequestURI().indexOf("admin")>=0){
            return "/exception/admin/error";
        }else {
            return "/exception/error";
        }
    }

    @Override
    public String getErrorPath() {
        return ERROR;
    }

}
